package com.cskaoyan.service;

import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;

import com.cskaoyan.module.bean.MarketTopicInfo;
import com.cskaoyan.module.pojo.*;
import com.cskaoyan.module.vo.MarketTopicBo;
import com.cskaoyan.module.vo.MarketTopicVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * TopicServiceImpl
 *
 * @param
 * @return
 * @2022/4/7
 **/
@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Override
    public MarketTopicVo list(MarketTopicBo params) {
        MarketTopicVo result=new MarketTopicVo();
        PageHelper.startPage(params.getPage(),params.getLimit());
        MarketTopicExample example=new MarketTopicExample();
        example.setOrderByClause(params.getSort()+ " "+params.getOrder());
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if ( params.getTitle()!=null) {
            criteria.andTitleLike("%"+params.getTitle()+"%");
        }
        if (params.getSubtitle()!=null) {
            criteria.andSubtitleLike("%"+params.getSubtitle()+"%");
        }
        List<MarketTopic> list1 = marketTopicMapper.selectByExample(example);
        List<MarketTopic> list=new ArrayList<>();
        for (MarketTopic marketTopic : list1) {

            list.add(marketTopic);
        }
        System.out.println("List<MarketTopic> list = " + list);
        PageInfo<MarketTopic> pageInfo=new PageInfo<>(list);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        int pageNum = pageInfo.getPageNum();
        result.setList(list);
        result.setLimit(pageInfo.getSize());
        //        page? pages?
        result.setPage(pageNum);
        result.setPages(pages);
        result.setTotal((int) total);
        System.out.println("MarketTopicVo result = " + result);
        return result;
    }
    @Override
    @Transactional
    public MarketTopic create(MarketTopic params) {
//        insert 并保存最大的id 再通过id查询
        params.setAddTime(new Date());
        params.setUpdateTime(new Date());
        params.setDeleted(false);
        int insertTopic = marketTopicMapper.insertSelective(params);
        System.out.println("MarketTopic after insert maxid = " + params.getId());
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(params.getId());
        return marketTopic;
    }
    @Override
    @Transactional
    public MarketTopicInfo read(Integer id) {
        MarketTopicInfo result = new MarketTopicInfo();
//       编辑专题的数据
        MarketTopic record = marketTopicMapper.selectByPrimaryKey(id);
        System.out.println("MarketTopicInfo read record。id = " + record.getId());
        result.setTopic(record);
        List<MarketGoods> list = new ArrayList<>();
        Integer[] goodsIds = record.getGoods();//拿到goods的id
        for (Integer goodsId : goodsIds) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(goodsId);
            if (marketGoods.getDeleted() != true) {
                list.add(marketGoods);
            }
        }
        result.setGoodsList(list);
        System.out.println("MarketTopicInfo Goods list = " + list);
        record.setAddTime(new Date());//MarketTopic设置加入时间
        record.setUpdateTime(new Date());//MarketTopic设置更新时间
        int update = marketTopicMapper.updateByPrimaryKey(record);
//        仅当删除状态为false才能加入到result中
//        example.setOrderByClause(params.);
        System.out.println("MarketTopicInfo read list = " + list);
        return result;
    }
    @Override
    @Transactional
    public MarketTopic update(MarketTopic params) {

        params.setUpdateTime(new Date());
        int update = marketTopicMapper.updateByPrimaryKeySelective(params);
        MarketTopic marketTopic  = marketTopicMapper.selectByPrimaryKey(params.getId());
        return marketTopic;
    }

    @Override
    public Integer delete(MarketTopic params) {
        params.setDeleted(true);
        params.setUpdateTime(new Date());
        int update = marketTopicMapper.updateByPrimaryKeySelective(params);
        return update;
    }

    @Override
    public Integer batchDelete(List<Integer> ids) {
        int update=0;
        for (Integer id : ids) {

            MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
            marketTopic.setDeleted(true);
            update=marketTopicMapper.updateByPrimaryKey(marketTopic);
        }
        return update;
    }

}
